package com.jason.mvptest;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.jason.mvptest.bean.User;
import com.jason.mvptest.presenter.UserLoginPresenter;
import com.jason.mvptest.view.UserLoginView;

public class MainActivity extends AppCompatActivity implements UserLoginView {


    private EditText et_username;
    private EditText et_password;
    private ProgressDialog mProgressDialog;
    private Button bt_clear, bt_login;

    private UserLoginPresenter mUserLoginPresenter = new UserLoginPresenter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mProgressDialog = new ProgressDialog(this);
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        bt_clear = (Button) findViewById(R.id.bt_clear);
        bt_login = (Button) findViewById(R.id.bt_login);

        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUserLoginPresenter.login();
            }
        });

        bt_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUserLoginPresenter.clear();
            }
        });


    }

    @Override
    public String getUserName() {
        return et_username.getText().toString().trim();
    }

    @Override
    public String getPassWord() {
        return et_password.getText().toString().trim();
    }

    @Override
    public void clearUserName() {
        et_username.setText("");

    }

    @Override
    public void clearPassWord() {
        et_password.setText("");
    }

    @Override
    public void showLoading() {
        if (mProgressDialog != null && !mProgressDialog.isShowing()) {
           mProgressDialog.show();
        }
    }

    @Override
    public void hideLoading() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void toMainActivity(User user) {
        Toast.makeText(this, user.getUsername() + " login success ", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loginErrorShow() {
        Toast.makeText(this, "登陆失败", Toast.LENGTH_SHORT).show();
    }
}
